package com.coffee.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coffee.app.entity.AppUser;
import com.coffee.app.entity.dto.AppUserQueryDTO;
import com.coffee.app.entity.vo.AppUserListVO;
import com.coffee.app.entity.vo.AppUserVO;
import com.coffee.app.mapper.AppUserMapper;
import com.coffee.app.service.IAppUserService;
import com.coffee.framework.common.util.StpAppUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Kevin
 * @since 2022-05-03
 */
@Service
@AllArgsConstructor
public class AppUserServiceImpl implements IAppUserService {

    private AppUserMapper appUserMapper;

    @Override
    public Page<AppUserListVO> page(Page reqPage, AppUserQueryDTO req) {
        LambdaQueryWrapper<AppUser> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.like(StrUtil.isNotBlank(req.getPhone()), AppUser::getPhone, req.getPhone());
        Page<AppUser> page = appUserMapper.selectPage(reqPage, queryWrapper);
        Page<AppUserListVO> voPage = BeanUtil.copyProperties(page, Page.class);
        voPage.setRecords(BeanUtil.copyToList(page.getRecords(), AppUserListVO.class));
        return voPage;
    }

    @Override
    public AppUserVO getUserInfo() {
        AppUser appUser = appUserMapper.selectById(StpAppUtil.getLoginIdAsLong());
        return BeanUtil.copyProperties(appUser, AppUserVO.class);
    }

}
